package com.tbit.uqbike.webmanager.controller;

import com.tbit.uqbike.constant.MachineFunctionConstant;
import com.tbit.uqbike.constant.enums.MachineFunctionModeEnum;
import com.tbit.uqbike.object.pojo.MachineFunction;
import com.tbit.uqbike.object.pojo.vo.BatchOperationMsgVO;
import com.tbit.uqbike.object.view.JsonResult;
import com.tbit.common.entity.view.Result;
import com.tbit.uqbike.service.business.MachineFunctionService;
import com.tbit.uqbike.webmanager.util.Assert;
import com.tbit.uqbike.webmanager.util.excel.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @Author: chen
 * @Description: 设备功能绑定
 * @Date: 创建于 10:12 2020/6/3
 */
@RestController
@RequestMapping("/function")
public class MachineFunctionController {

    @Autowired
    private MachineFunctionService machineFunctionService;

    /**
     * 分页查询
     *
     * @param machineNO
     * @return
     */
    @RequestMapping("/queryPage")
    public JsonResult queryPage(Integer accountId, String machineNO, Integer functionType, @RequestParam(defaultValue = "1") Integer pageNO, @RequestParam(defaultValue = "10") Integer rowCount) {
        List<MachineFunction> list = machineFunctionService.queryPage(accountId, machineNO, functionType, pageNO, rowCount);
        Integer total = machineFunctionService.queryCount(accountId, machineNO, functionType);
        return JsonResult.ok(list, total);
    }

    /**
     * 根据功能类型查看关联的模式
     * @param functionType 功能类型
     * @return 1
     */
    @GetMapping("/mode/{functionType}")
    public Result<List<Map<String, String>>> selectFunctionModeByFunctionType(@PathVariable Integer functionType) {
        List<Map<String, String>> modeMap = MachineFunctionModeEnum.getFunctionModeByType(functionType);
        return Result.success(modeMap);
    }


    /**
     * 批量添加
     *
     * @param machineNOs
     */
    @RequestMapping("/addBatch")
    public JsonResult addBatch(String machineNOs, Integer functionType, String functionName, Integer functionValue,Integer functionMode) {
        Assert.notNull(machineNOs, "ver.machineNOs.notNull");
        Assert.notNull(functionType, "ver.functionType.notNull");
        Assert.notNull(functionName, "ver.functionName.notNull");
        Assert.notNull(functionValue, "ver.functionValue.notNull");
        if (functionType == MachineFunctionConstant.PHOTO_RETURN_CAR) {
            Assert.notNull(functionMode, "拍照还车模型不能为空");
        }
        List<String> list = Arrays.asList(machineNOs.split(","));
        BatchOperationMsgVO<String> batchOperationMsgVO = machineFunctionService.addBatch(list, functionType, functionName, functionValue,functionMode);
        return JsonResult.ok(batchOperationMsgVO);
    }

    /**
     * 批量删除
     *
     * @param machineNOs
     */
    @RequestMapping("/delBatch")
    public JsonResult delBatch(String machineNOs, Integer functionType) {
        Assert.notNull(machineNOs, "ver.machineNOs.notNull");
        Assert.notNull(functionType, "ver.functionType.notNull");
        List<String> list = Arrays.asList(machineNOs.split(","));
        BatchOperationMsgVO<String> batchOperationMsgVO = machineFunctionService.delBatch(list, functionType);
        return JsonResult.ok(batchOperationMsgVO);
    }

    /**
     * 导出文件
     *
     * @param accountId
     * @param functionType
     * @return
     */
    @GetMapping("/export")
    public JsonResult export(Integer accountId, Integer functionType) {
        List<MachineFunction> list = machineFunctionService.getByType(accountId, functionType);
        ExcelUtil<MachineFunction> util = new ExcelUtil<MachineFunction>(MachineFunction.class);
        return util.exportExcel(list, "machineFunction");
    }

}
